/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.Facade;
import javax.inject.Named;
import javax.enterprise.context.ConversationScoped;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.inject.Inject;
import model.dto.ApplicationDTO;
import model.dto.PersonDTO;
import model.integration.CompetenceList;
import model.integration.DateList;

/**
 *
 * @author Ahmed Salem
 */
@Named(value = "loginBean")
@ConversationScoped
public class LoginBean implements Serializable {

    private static final long serialVersionUID = 16247164405L;
    @EJB
    private Facade controller; 
   
    
    @Inject
    private Conversation conversation;
    private String username;
    private String password;
    private String adminName;
    private boolean loginStatus = false;
    private List<DateList> dl;
    private List<CompetenceList> comList;
    /**
     *
     */
    public LoginBean() {
    }
    
    // This method starts the conversation 
     private void startConversation() 
    {
        if (conversation.isTransient()) 
        {
            conversation.begin();
        }
    }

   // This method stops the conversation  
    private void stopConversation() 
    {
        if (!conversation.isTransient()) 
        {
            conversation.end();
        }
    }
    
    /**
     *
     * @param adminName sets the admin name 
     * 
     */
    public void setAdminName(String adminName){
        this.adminName = adminName;
    }
    
    /**
     *
     * @return the admin name 
     */
    public String getAdminName(){
        return this.adminName;
    }
    
    /**
     *
     * @param username
     */
    public void setUserName(String username) 
    {
        this.username = username;
    }
    
    /**
     *
     * @return username 
     */
    public String getUserName() 
    {
        return this.username;
    }
    
    /**
     *
     * @param password
     */
    public void setPassword(String password) 
    {
        this.password = password;
    }
    
    /**
     *
     * @return the password from the user
     */
    public String getPassword() 
    {
        return this.password;
    }
    
    /**
     *
     * @param loginStatus 
     */
    public void setLoginStatus(boolean loginStatus) {
        this.loginStatus = loginStatus;
    }
    
     /**
     *
     * @return a boolean , loginStatus 
     */
    public boolean getLoginStatus() {
        return this.loginStatus;
    }
    
    /**
     *
     * @return List of ApplicationsDTO to show applications
     */
    public List<ApplicationDTO>ShowApplications(){
         startConversation();
    
         List <ApplicationDTO> apdto = controller.getApplicationsFacade();
         return controller.getApplicationsFacade();
     }
     
    /**
     *sets the values of loginStatus to false 
     * to logout the user
     */
    public void logOut(){
       if(loginStatus == true){
       this.loginStatus = false;}
     }
     
    /**
     *
     * @return a boolean if the user exist. if true the user can log in
     *
     */
    public void logIn(){
      PersonDTO p = controller.logInFacade(username,password);
      setAdminName(p.getName());
      setLoginStatus(true);
   }
   
    /**
     *
     * @return a list of DateList object
     */
    public List<DateList>getAv(){
       
       return dl;
   }
 
}
